import Datastore from 'nedb-promises'
import {
  InsertData, DeleteData, UpdateData, SearchData,
  InsertDataSync, DeleteDataSync, UpdateDataSync, SearchDataSync,
  SelectDatabase,
} from '@/utils/db'
import {
  GetDictionaryItemName,
  GetDictionaryItemNameByArray,
  GetCustomizeDictionaryItemNameByArray,
  GetDictionaryArrayByValue
} from '@/utils/system'

/**
 * 添加bug方法
 * @param request
 * @param response
 */
const insertBugItem = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  let { id } = queryParams
  const _db = SelectDatabase('bug-manager', 'test', 'BugList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}

/**
 * 获取所有bug列表
 * @param request
 * @param response
 */
const getBugList = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'test', 'BugList')
  let d_BugType = await GetDictionaryArrayByValue('BugType')
  let d_Priority = await GetDictionaryArrayByValue('Priority')
  let d_Steps = await GetDictionaryArrayByValue('Steps')

  const sendData = (data: ResponseDataType) => {
    let dataArr: any[] = data.data
    dataArr = dataArr.map((item: any) => {
      return {
        ...item,
        bugType: GetDictionaryItemNameByArray(item.bugType, d_BugType),
        priority: GetDictionaryItemNameByArray(item.priority, d_Priority),
        steps: GetDictionaryItemNameByArray(item.steps, d_Steps),
      }
    })
    data.data = dataArr
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 获取bug详情
 * @param request
 * @param response
 */
const getBugItem = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'test', 'BugList')

  const sendData = async (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 删除单个bug
 * @param request
 * @param response
 */
const deleteBugItem = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '删除失败',
    data: [],
    total: -1,
  }

  let { id } = queryParams
  const _db = SelectDatabase('bug-manager', 'test', 'BugList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    DeleteData(_db, queryParams, resJSON, sendData)
  }
}

export default [
  {
    path: '/bugManager/test/insertBugItem',
    func: insertBugItem,
  },
  {
    path: '/bugManager/test/getBugList',
    func: getBugList
  },
  {
    path: '/bugManager/test/getBugItem',
    func: getBugItem
  },
  {
    path: '/bugManager/test/deleteBugItem',
    func: deleteBugItem
  },
]